package gameFrame;

import com.ballgame.Plane;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Date;

import static gameFrame.GameUtil02.*;
public class MyGameFrame02 extends Frame {

    Image bgImg = GameUtil02.getImage("images01/bg.jpg");
    Image planeImg = GameUtil02.getImage("images01/plane.png");
    int x= 300;//飞机坐标
    int y = 180;
    Date startTime = new Date();//游戏开始计时
    Date endTime;
    int period;//游戏时间
    int count=1;
    //声明对象,没有new()创建对象，因为这里new爆炸图片没用，碰撞时爆炸才new
    Explode explode;
    plane02 plane02 =  new plane02(planeImg,200,200,7);
    //炮弹创建对象

    Shell02[] shell02 = new Shell02[10];

   /* int numShell;
    public int  numShell00 (){

        if(count % 20 ==0) {
            numShell = (int) (Math.random() * 16);
        }

        return numShell;
    }*/
   // Shell02[] shell02 = new Shell02[numShell];

    //初始化窗口
    public void launchFrame(){
        this.setTitle("飞机大战");
        this.setVisible(true);//窗口默认不可见，需要让他可见
        this.setSize(FRAME_WIDTH,FRAME_HIGHT);
        this.setLocation(300,100);//窗口位置
        //窗口关闭按钮  内部类
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
               System.exit(0);//退出
            }
        });
        //调用，启动窗口线程
        new PaintThread().start();
        //启动键盘监听
        this.addKeyListener(new KeyMonitor());
        //初始化炮弹,再画出来

            for (int i = 0; i < shell02.length; i++) {
                shell02[i] = new Shell02();
            }

    }

    @Override
    public void paint(Graphics g) {
        g.drawImage(bgImg,0,0,FRAME_WIDTH,FRAME_HIGHT,null);
        //g.drawImage(planeImg,x,y,30,30,null);
        plane02.drawMySelf(g);
        //创建对象后，画出来
        for(int i=0;i<shell02.length;i++) {
            shell02[i].drawMySelf(g);

            boolean peng = shell02[i].getRec().intersects(plane02.getRec());
            if(peng){

                plane02.live = false;
                endTime = new Date();
                period = (int)(endTime.getTime()-startTime.getTime())/1000;
                if(explode == null){
                    explode = new Explode(plane02.x, plane02.y);
                }
                explode.draw(g);
                printInfo(g,"飞机爆炸",20,plane02.x, plane02.y,Color.WHITE);
            }
        }
        /*for(int n=0;n<=shell02.length;n++) {
            for (Shell02 sh : shell02) {
                if (sh.y > 500) {
                   // remove();
                }
            }
        }*/
        if(!plane02.live){
            printInfo(g,"游戏时间："+period+"秒",40,200,200,Color.WHITE);
        }
    }
    public void printInfo(Graphics g,String str,int size,int x,int y,Color color){
       Font oldFont = g.getFont();
       Color oldColor = g.getColor();

        Font f = new Font("宋体",Font.BOLD,size);
        g.setFont(f);
        g.setColor(Color.WHITE);
        g.drawString(str,x,y);

        g.setColor(oldColor);
        g.setFont(oldFont);
    }
    //键盘监听内部类  在初始化窗口调用
    //键盘方向键的应用，1，键盘监听内部类 2，在飞机类定义方向，
    // 3，增加方向的方法 public void addDirection(KeyEvent e)  (KeyEvent e)将按的键传入来
    //4,用switch(e.getKeyCode())  case KeyEvent.Vk_LEFT 判定传入的键left,right,up,down
    //5,减方向public void minusDirection(KeyEvent e){
    // switch(e.getKeyCode()){
     //   case KeyEvent.Vk_LEFT:
    //      left = false;
        // }
    // }
    //飞机移动 public void drawMySelf(Graphics g){
    //  if(left){x -=speed;}
    // }
    class KeyMonitor extends KeyAdapter{

        //按下
        @Override
        public void keyPressed(KeyEvent e) {
           plane02.addDirection(e);
        }
        //松开
        @Override
        public void keyReleased(KeyEvent e) {
            plane02.minusDirection(e);
        }
    }

    //动画，内部类，重画线程，无限循环，  定义线程后要调用
    class PaintThread extends Thread {

        @Override
        public void run() {
            while (true){
                repaint();
                try {
                    Thread.sleep(40);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
               // count++;
            }

        }

    }

    public static void main(String[] args) {
        MyGameFrame02 frame02 = new MyGameFrame02();
        frame02.launchFrame();
    }

        //解决闪屏
        private Image offScreenImage = null;

        public void update (Graphics g){
            if (offScreenImage == null)
                offScreenImage = this.createImage(FRAME_WIDTH, FRAME_HIGHT); //窗口宽度，高度

            Graphics g0ff = offScreenImage.getGraphics();
            paint(g0ff);
            g.drawImage(offScreenImage, 0, 0, null);

        }
}
